package com.windchime.blog.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.windchime.blog.entity.Resource;
import com.windchime.blog.entity.ResourceType;
import com.windchime.blog.entity.vo.ResourceSearch;
import com.windchime.blog.mapper.ResourceMapper;
import com.windchime.blog.service.ResourceService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.windchime.blog.service.ResourceTypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * <p>
 * 资源表 服务实现类
 * </p>
 *
 * @author WindChime
 * @since 2021-08-08
 */
@Service
public class ResourceServiceImpl extends ServiceImpl<ResourceMapper, Resource> implements ResourceService {

    @Autowired
    private ResourceTypeService resourceTypeService;

    /**
     * 分页查询-带条件
     * @param page  当前页
     * @param limit 当前页条目数
     * @param resourceSearch 条件
     * @return resources
     */
    @Override
    public PageInfo<Resource> selectResourceList(Integer page, Integer limit, ResourceSearch resourceSearch) {
        PageHelper.startPage(page, limit);
        QueryWrapper<Resource> wrapper = new QueryWrapper<>();
        wrapper.orderByDesc("create_time");
        if (!StringUtils.isEmpty(resourceSearch.getName())) {
            wrapper.like("name", resourceSearch.getName());
        }
        if (!StringUtils.isEmpty(resourceSearch.getOneTypeId())) {
            wrapper.eq("one_type_id", resourceSearch.getOneTypeId());
        }
        List<Resource> resources = baseMapper.selectList(wrapper);
        for (Resource resource : resources) {
            resource.setOneTypeName(resourceTypeService.getOne(new QueryWrapper<ResourceType>().eq("flag", false).eq("id", resource.getOneTypeId())).getName());
            ResourceType two = resourceTypeService.getOne(new QueryWrapper<ResourceType>().eq("flag", true).eq("id", resource.getTwoTypeId()));
            if (two == null) {
                resource.setTwoTypeName("无分类");
            } else {
                resource.setTwoTypeName(two.getName());
            }
        }
        return new PageInfo<>(resources);
    }

    /**
     * 一级分类下有多少资源
     * @param id 一级分类ID
     * @return num
     */
    @Override
    public Integer selectOneTypeHasResources(Integer id) {
        QueryWrapper<Resource> wrapper = new QueryWrapper<>();
        wrapper.eq("one_type_id", id);
        return baseMapper.selectCount(wrapper);
    }

    /**
     * 资源展示--以资源分类为组--前台所用
     * @return resourceSplit
     */
    @Override
    public Map<String, List<Resource>> resourceSplit() {
        List<Integer> oneTypeIds = baseMapper.resourceSplit();
        List<ResourceType> resourceTypes = oneTypeIds.stream().map(oneTypeId -> resourceTypeService.getOne(new QueryWrapper<ResourceType>().eq("id", oneTypeId).eq("flag", false))).collect(Collectors.toList());

        Map<String, List<Resource>> map = new HashMap<>();
        for (ResourceType resourceType : resourceTypes) {
            Integer id = resourceType.getId();
            List<Resource> resources = baseMapper.selectList(new QueryWrapper<Resource>().eq("one_type_id", id));
            for (Resource resource : resources) {
                //设置二级分类名
                ResourceType res = resourceTypeService.getOne(new QueryWrapper<ResourceType>().eq("id", resource.getTwoTypeId()).eq("flag", true));
                resource.setTwoTypeName(res.getName());
            }
            map.put(resourceType.getName(), resources);
        }

        return map;
    }
}
